package com.ysg.utils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.ysg.widget.pager.NoSwipeViewPager;
import com.ysg.widget.tab.CommonTabLayout;
import com.ysg.widget.tab.CustomTabEntity;
import com.ysg.widget.tab.OnTabSelectListener;
import com.ysg.widget.tab.TabEntity;
import java.util.ArrayList;

/**
 * tab的两种viewPager,fragment
 */
public class YTabUtil {
    public interface OnPositionListener{
        void onPosition(int position);
    }

    public static void initText(String[] titles, CommonTabLayout tabLayout, ViewPager viewPager, PagerAdapter adapter, OnPositionListener listener){
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            tabEntities.add(new TabEntity(titles[i], 0, 0));
        }
        init(titles, tabLayout, viewPager, adapter, tabEntities, listener);
    }

    /**
     * xml中的FrameLayout的id为fragmentManager
     */
    public static void initTextByFragment(FragmentActivity fragmentActivity, CommonTabLayout tabLayout, ArrayList<Fragment> fragments, String[] titles, int fragmentId,OnPositionListener listener){
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            tabEntities.add(new TabEntity(titles[i], 0, 0));
        }
        tabLayout.setTabData(tabEntities, fragmentActivity, fragmentId, fragments);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (listener != null){
                    listener.onPosition(position);
                }
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    public static void initIconText(CommonTabLayout tabLayout, ViewPager viewPager, PagerAdapter adapter, String[] titles, int[] selectIds, int[] unselectIds){
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            tabEntities.add(new TabEntity(titles[i], selectIds[i], unselectIds[i]));
        }
        init(titles, tabLayout, viewPager, adapter, tabEntities, null);
    }

    private static void init(String[] titles, CommonTabLayout tabLayout, ViewPager viewPager, PagerAdapter adapter, ArrayList<CustomTabEntity> tabEntities, OnPositionListener listener) {
        tabLayout.setTabData(tabEntities);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
                if (listener != null){
                    listener.onPosition(position);
                }
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
        //viewPager
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setOffscreenPageLimit(titles.length - 1);

        //禁止滑动
        if (viewPager instanceof NoSwipeViewPager){
            NoSwipeViewPager noSwipeViewPager = (NoSwipeViewPager)viewPager;
            noSwipeViewPager.setCanSwipe(false);
        }
    }

}
